package br.skytef.stmanager.daos;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import br.skytef.stmanager.entities.ComercialClienteEntity;
import br.skytef.stmanager.utils.Pool;

public class ComercialClienteDAO {
	private java.sql.Connection conexao;

	public boolean inserir(ComercialClienteEntity cc){
		try {
			conexao = Pool.getConnection();
			String query = "INSERT INTO comercialCliente(cliente_idCliente, Comercial_id) VALUES (?,?);";
			PreparedStatement preparedStmt = conexao.prepareStatement(query);
			preparedStmt.setInt 		(1, cc.getClienteID());
			preparedStmt.setInt 		(2, cc.getComercialID());
			preparedStmt.execute();
			conexao.close();
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public ComercialClienteEntity buscar(int idCliente, int idComercial) {
		ComercialClienteEntity cc = null;
		try {

			conexao = Pool.getConnection();
			Statement st = conexao.createStatement();
			ResultSet rs = st.executeQuery("SELECT * FROM comercialCliente WHERE cliente_idCliente = "+idCliente+" AND Comercial_id = "+idComercial+";");
			if (rs.next()) {                    
				cc = new ComercialClienteEntity();
				cc.setId(rs.getInt("id"));
				cc.setClienteID (rs.getInt("cliente_idCliente"));
				cc.setComercialID(rs.getInt("Comercial_id"));
			}
			conexao.close();			
		}catch (SQLException e) {
			System.out.println("SQL erro: "+ e);
			return null;
		}
		return cc;
	}

}
